//
//  main.m
//  PredicateDemo
//
//  Created by shk on 14/12/10.
//  Copyright (c) 2014年 com.shk. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        
        NSMutableArray *array = [NSMutableArray array];
        for(int i=0; i<10; i++){
            Person *person = [[Person alloc] init];
            if(i<5){
                person.name = [NSString stringWithFormat:@"jack-%d", i];
            }else{
                person.name = [NSString stringWithFormat:@"tom-%d", i];
            }
            person.age = @(20+i);
            [array addObject:person];
        }
        
        //判断是否满足条件
        //单个条件
        //NSPredicate *perdicate = [NSPredicate predicateWithFormat:@"age<25"];
        
        //首字母条件
//        NSPredicate *perdicate = [NSPredicate predicateWithFormat:@"name BEGINSWITH 'tom'"];
        
        //like条件
        NSPredicate *perdicate = [NSPredicate predicateWithFormat:@"name like '*-*'"];
        
        //多个条件,AND OR || &&都可以
//        NSPredicate *perdicate = [NSPredicate predicateWithFormat:@"age>25 and name='tom-6'"];
        
        //in条件
//        NSArray *fileterInArray = @[@"jack-1",@"tom-7",@"xxx"];
//        NSPredicate *perdicate = [NSPredicate predicateWithFormat:@"name in %@", fileterInArray];
        
        for(Person *p in array){
            BOOL result = [perdicate evaluateWithObject:p];
            if(result){
                NSLog(@"%@", p);
            }
        }
        
        //对数组过滤
        NSArray *filterArray = [array filteredArrayUsingPredicate:perdicate];
        NSLog(@"%@", filterArray);
        
        
    }
    return 0;
}
